package Class;

import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;
import java.util.*;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

//create account: login name and random password
public class CreateAccount {
	
	//get day from database
	public static java.sql.Date Date()
	{
		java.sql.Date current = null;
		try
		{
		Statement stm = ConnectDatabase.connection.createStatement();
		ResultSet rs = stm.executeQuery("SELECT NowDate FROM TempTable");
		while(rs.next())
		{
			current = rs.getDate(1);
		}
		} catch(Exception ex) 
		{
			System.out.println(ex.toString());
		}
		return current;
	}
	
	//get ID from database
	public static int ID(String loginName)
	{
		int id = 0;
		try
		{
			Statement stm = ConnectDatabase.connection.createStatement();
			ResultSet rs = stm.executeQuery("SELECT IDEmp FROM TempTable");
			while(rs.next())
			{
				id = rs.getInt(1);
			}
		} catch(Exception ex)
		{
			System.out.println(ex.toString());
		}
		return id;
	}
	
	//create login name follow id
	public static String LoginName()
	{
		String loginname = "";
		try
		{
			String sql = "{call CreateLoginName(?)}";
			CallableStatement cstmt = ConnectDatabase.connection.prepareCall(sql);
			cstmt.registerOutParameter(1,java.sql.Types.VARCHAR);
			cstmt.execute();
			loginname = cstmt.getString(1);
		} catch(Exception ex) {System.out.print(ex.toString());}
		return loginname;
	}
	
	//create random password
	public static String randomPassword()
	{
		char[] password = new char[6];
		String resultPassword = "";
		Random randNumber = new Random();
		Random randType = new Random();
		for(int i = 0;i<6;i++)
		{
			int type = randType.nextInt(3) + 1;
			switch(type)
			{
				case 1:
					char number = (char) (randNumber.nextInt(10) + 48);
					password[i] = number; 
					break;
				case 2:
					char upperChar = (char) (randNumber.nextInt(26) + 65);
					password[i] = upperChar;
					break;
				case 3:
					char lowerChar = (char) (randNumber.nextInt(26) + 97);
					password[i] = lowerChar;
					break;
			}
		}
		for(Character c : password)
		{
			resultPassword += c.toString();
		}
		return resultPassword;
	}
	
	//create avatar
	public static String Avatar(String loginName,File fileAvatar,Component component)
	{
		String des = null;
		File targetFileAvatar;
		File dir = new File("emps");
		if(!dir.exists())
		{
			dir.mkdir();
		}
		if(fileAvatar != null)
		{
//			try
//			{
//				des = loginName+".jpg";
//				targetFileAvatar = new File("emps\\" + des);
//				BufferedImage originalImage = ImageIO.read(fileAvatar);
//				int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB_PRE : originalImage.getType();
//				BufferedImage resizedImage = new BufferedImage(80, 80, type);
//			    Graphics2D g = resizedImage.createGraphics();
//			    g.drawImage(originalImage, 0, 0, 80, 80, null);
//			    g.dispose();
//			    ImageIO.write(resizedImage, "jpg", targetFileAvatar);
//			} catch(Exception ex) {
//				System.out.println(ex.toString());
//			}
			try
			{
				BufferedImage img = new BufferedImage(80, 80, BufferedImage.TYPE_INT_RGB);
				component.paint(img.getGraphics());
				des = loginName+".jpg";
				targetFileAvatar = new File("emps\\" + des);
				ImageIO.write(img, "jpg", targetFileAvatar);
//				InputStream is = null;
//		        OutputStream os = null;
//	            is = new FileInputStream(fileAvatar);
//	            os = new FileOutputStream(targetFileAvatar);
//	            byte[] buffer = new byte[1024];
//	            int length;
//	            while ((length = is.read(buffer)) > 0) {
//	                os.write(buffer, 0, length);
//	            }
//	            
//	            is.close();
//	            os.close();
	        } 
	        catch(Exception ex) {}
		}
		else 
		{
			//TODO
			des = null;
		}	
		return des;
	}
}
